package mongodb

import (
	"context"
	"github.com/pingcap/log"
	"go.mongodb.org/mongo-driver/mongo"
	"go.mongodb.org/mongo-driver/mongo/options"
	"go.uber.org/zap"
	"net/url"
)

type FactoryCreator func(ctx context.Context, sinkUrl *url.URL) (*mongo.Client, error)

func NewCreatorFactory(ctx context.Context, sinkUrl *url.URL) (*mongo.Client, error) {
	option := options.Client().ApplyURI(sinkUrl.String())

	mongoClient, err := mongo.Connect(ctx, option)
	if err != nil {
		log.Error("DML sink mongodb connect fail", zap.Error(err))
	}

	db := mongoClient.Database("ticdc")
	if db == nil {
		log.Error("DML sink mongodb database open fail")
	}

	return mongoClient, nil
}
